<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <div>
                    <el-input v-model="query.keywords" placeholder="关键词" class="handle-input mr10"></el-input>
                    <el-button type="primary" size="small" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                </div>
                <div>
                    <el-button type="primary" size="small" icon="el-icon-plus" @click="handleCreate" class="add-btn">
                        添加部门</el-button>
                </div>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header"
                :cell-style="{padding:'3px 0'}">
                <el-table-column prop="depart_id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="depart_name" label="部门名称"></el-table-column>
                <el-table-column prop="depart_simple_name" label="部门简称"></el-table-column>
                <el-table-column label="部门性质">
                    <template #default="scope">{{ getShape(scope.row.depart_shape) }}</template>
                </el-table-column>
                <el-table-column prop="depart_id_yy" label="医院统一编码"></el-table-column>
                <el-table-column prop="depart_id_his" label="HIS对应ID"></el-table-column>
                <!-- <el-table-column prop="depart_id_emr" label="EMR对应ID"></el-table-column>
                <el-table-column prop="depart_id_lis" label="LIS对应ID"></el-table-column>
                <el-table-column prop="depart_id_pacs" label="PACS对应ID"></el-table-column> -->

                <el-table-column label="操作" width="230" align="center">
                    <template #default="scope">
                        <el-button type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button type="danger" size="small" @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.page"
                    :page-size="query.limit" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
            </div>
        </div>

        <!-- 添加弹出框 -->
        <el-dialog title="添加" v-model="addVisible" width="50%">
            <el-form label-width="110px">
                <el-form-item label="部门名称">
                    <el-input v-model="createForm.depart_name"></el-input>
                </el-form-item>
                <!-- <el-form-item label="部门简称">
                    <el-input v-model="createForm.depart_simple_name"></el-input>
                </el-form-item> -->
                <el-form-item label="部门性质 ">
                    <el-select v-model="createForm.depart_shape" class="m-2" placeholder="Select" size="large">
                        <el-option
                          v-for="item in shapes"
                          :key="item.id"
                          :label="item.title"
                          :value="item.id"
                        />
                      </el-select>
                </el-form-item>
                <el-form-item label="是否可以删除 ">
                    <el-radio-group v-model="createForm.can_del_state" class="ml-4">
                        <el-radio label="1" size="large">是</el-radio>
                        <el-radio label="0" size="large">否</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="医院统一编码">
                    <el-input v-model="createForm.depart_id_yy"></el-input>
                </el-form-item>
                <el-form-item label="HIS对应ID">
                    <el-input v-model="createForm.depart_id_his"></el-input>
                </el-form-item>
                <!-- <el-form-item label="EMR对应ID">
                    <el-input v-model="createForm.depart_id_emr"></el-input>
                </el-form-item>
                <el-form-item label="LIS对应ID">
                    <el-input v-model="createForm.depart_id_lis"></el-input>
                </el-form-item>
                <el-form-item label="PACS对应ID ">
                    <el-input v-model="createForm.depart_id_pacs"></el-input>
                </el-form-item> -->
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="addVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveCreate">确 定</el-button>
                </span>
            </template>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" v-model="editVisible" width="70%">
            <el-form label-width="110px">
                <el-form-item label="部门名称">
                    <el-input v-model="eidtForm.info.depart_name"></el-input>
                </el-form-item>
                <!-- <el-form-item label="部门简称">
                    <el-input v-model="eidtForm.info.depart_simple_name"></el-input>
                </el-form-item> -->
                <el-form-item label="部门性质 ">
                    <el-select v-model="eidtForm.info.depart_shape" class="m-2" placeholder="Select" size="large">
                        <el-option
                          v-for="item in shapes"
                          :key="item.id"
                          :label="item.title"
                          :value="item.id"
                        />
                      </el-select>
                </el-form-item>

                <el-form-item label="是否可以删除 ">
                    <el-radio-group v-model="eidtForm.info.can_del_state" class="ml-4">
                        <el-radio label="1" size="large">是</el-radio>
                        <el-radio label="0" size="large">否</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="医院统一编码">
                    <el-input v-model="eidtForm.info.depart_id_yy"></el-input>
                </el-form-item>
                <el-form-item label="HIS对应ID">
                    <el-input v-model="eidtForm.info.depart_id_his"></el-input>
                </el-form-item>
                <!-- <el-form-item label="EMR对应ID">
                    <el-input v-model="eidtForm.info.depart_id_emr"></el-input>
                </el-form-item>
                <el-form-item label="LIS对应ID">
                    <el-input v-model="eidtForm.info.depart_id_lis"></el-input>
                </el-form-item>
                <el-form-item label="PACS对应ID">
                    <el-input v-model="eidtForm.info.depart_id_pacs"></el-input>
                </el-form-item> -->
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
    import { ref, reactive } from "vue";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { departList, departCreate, departUpdate } from "../api/index";
    import { typeName, log } from "../utils/user"

    export default {
        name: "basetable",
        setup() {
            const statusText = ["禁用", "启用"]
            const typeNames = typeName();

            let typeText = {};
            Object.keys(typeNames).forEach(k => {
                typeText[typeNames[k][0]] = typeNames[k][1]
            })
            log("typeText", typeText);

            const query = reactive({
                keywords: "",
                page: 1,
                limit: 15,
            });
            const tableData = ref([]);
            const pageTotal = ref(0);

            // 获取表格数据
            const getData = () => {
                departList(query).then((res) => {
                    if (res.status == 'ok') {
                        tableData.value = res.data.list;
                        pageTotal.value = res.data.pageTotal || 1;
                    } else {
                        ElMessage.error(res.msg)
                        return false;
                    }
                });
            };
            getData();

            // 查询操作
            const handleSearch = () => {
                query.page = 1;
                getData();
            };
            // 分页导航
            const handlePageChange = (val) => {
                query.page = val;
                getData();
            };

            // 删除操作
            const handleDelete = (index) => {
                // 二次确认删除
                ElMessageBox.confirm("确定要删除吗？", "提示", {
                    type: "warning",
                })
                    .then(() => {
                        ElMessage.success("删除成功");
                        tableData.value.splice(index, 1);
                    })
                    .catch(() => { });
            };
            // 表格添加
            const addVisible = ref(false);

            const handleCreate = () => {
                addVisible.value = true;
            };
            const shapes = [
                {
                    id: 1,
                    title: "住院临床科室"
                },
                {
                    id: 1,
                    title: "住院临床科室"
                },
                {
                    id: 2,
                    title: "门诊临床科室"
                },
                {
                    id: 3,
                    title: "门诊药房"
                },
                {
                    id: 4,
                    title: "住院药房"
                },
                {
                    id: 5,
                    title: "收费处"
                },
                {
                    id: 6,
                    title: "医技科室"
                },
                {
                    id: 7,
                    title: "药库"
                },
                {
                    id: 8,
                    title: "外单位"
                },
                {
                    id: 9,
                    title: "病区"
                },
                {
                    id: 10,
                    title: "住院临床科室"
                },
            ]

            let createForm = reactive({
                depart_name: "",
                // depart_simple_name: "",
                depart_shape: 9 ,
                can_del_state: "1",
                depart_id_yy: "",
                depart_id_his: "",
                // depart_id_emr: "",
                // depart_id_lis: "",
                // depart_id_pacs: ""
            });

            const saveCreate = () => {
                log(createForm)
                if (!createForm.depart_name) {
                    ElMessage.error('请输入部门名称')
                    return false;
                }

                departCreate(createForm).then(res => {
                    if (res.status == 'ok') {
                        addVisible.value = false;
                        ElMessage.success(res.msg);
                        query.pageIndex = 1;
                        getData();
                    } else {
                        ElMessage.error(res.msg)
                    }
                })
            }


            // 表格编辑时弹窗和保存
            const editVisible = ref(false);
            let eidtForm = reactive({
                id: 0,
                info: {
                    depart_name: "",
                    // depart_simple_name: "",
                    depart_shape: "1",
                    can_del_state: "0",
                    depart_id_yy: "",
                    depart_id_his: "",
                    // depart_id_emr: "",
                    // depart_id_lis: "",
                    // depart_id_pacs: ""
                }
            });
            let idx = -1;
            const handleEdit = (index, row) => {
                idx = index;
                eidtForm.info.depart_name = row["depart_name"];
                // eidtForm.info.depart_simple_name = row["depart_simple_name"];
                eidtForm.info.depart_shape =  parseInt(row["depart_shape"]);
                eidtForm.info.can_del_state = row["can_del"]+'';
                eidtForm.info.depart_id_yy = row["depart_id_yy"];
                eidtForm.info.depart_id_his = row["depart_id_his"];
                // eidtForm.info.depart_id_emr = row["depart_id_emr"];
                // eidtForm.info.depart_id_lis = row["depart_id_lis"];
                // eidtForm.info.depart_id_pacs = row["depart_id_pacs"];
                log(eidtForm)

                eidtForm.id = row["depart_id"];
                editVisible.value = true;
            };
            const saveEdit = () => {
                if (!eidtForm.info.depart_name) {
                    ElMessage.error('请输入部门名称')
                    return false;
                }
                departUpdate(eidtForm).then(res => {
                    if (res.status == 'ok') {
                        editVisible.value = false;
                        ElMessage.success(res.msg);
                        getData();
                    } else {
                        ElMessage.error(res.msg)
                    }
                })

            };
            const toggleStatus = (id, status) => {
                log("toggleStatus", id)
                let temp = {
                    id: id,
                    info: {
                        status: status
                    }
                }

                ElMessageBox.confirm("确认修改吗", "提示", {
                    type: "warning",
                })
                    .then(() => {
                        departUpdate(temp).then(res => {
                            if (res.status == 'ok') {
                                ElMessage.success(res.msg)
                                getData();
                                return false;
                            } else {
                                ElMessage.error(res.msg)
                                return false;
                            }
                        })
                    })
                    .catch(() => { });


            }
            const getShape = id => {
                let shape  = shapes.find(v => v.id == id);
                return shape.title
            }
            return {
                statusText,
                typeText,

                // data
                query,
                tableData,
                pageTotal,


                handleSearch,
                handlePageChange,
                handleDelete,

                editVisible,
                eidtForm,
                handleEdit,
                saveEdit,

                createForm,
                addVisible,
                handleCreate,
                saveCreate,

                toggleStatus,
                shapes,
                getShape
            };
        },
    };
</script>

<style scoped>
    .handle-box {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 8px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }

    .m-radio {
        margin: 12px;
    }
</style>